package com.hbsx.why;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.ui.activity.ActivityDetailActivity;
import com.hbsx.why.ui.activity.ActivityFragment;
import com.hbsx.why.ui.find.FindFragment;
import com.hbsx.why.ui.find.FindFragmentTwo;
import com.hbsx.why.ui.fujin.NearFragment;
import com.hbsx.why.ui.home.HomeFragment;
import com.hbsx.why.ui.mine.MineFragment;
import com.hbsx.why.ui.order.OrderFragment;
import com.hbsx.why.ui.space.SpaceFragment;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.PopWindow;
import com.hbsx.why.utils.alphatabs.AlphaTabsIndicator;
import com.hbsx.why.utils.alphatabs.OnTabChangedListner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author LIUB
 * @desc 主页面
 * @time 2017/12/25 10:44
 */

public class TabMainActivity extends BaseActivity {
    //    @BindView(R.id.main_text_tab)
//    TextView mainTextTab;
    private static AlphaTabsIndicator alphaTabsIndicator;
    @BindView(R.id.pop)
    View pop;
    private Context context;

    @Override
    public void initView() {
        context = this;
        ViewPager mViewPger = (ViewPager) findViewById(R.id.mViewPager);
        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager());
        mViewPger.setAdapter(mainAdapter);
        mViewPger.addOnPageChangeListener(mainAdapter);
        mViewPger.setOffscreenPageLimit(5);
        alphaTabsIndicator = (AlphaTabsIndicator) findViewById(R.id.alphaIndicator);
        alphaTabsIndicator.setViewPager(mViewPger);

        alphaTabsIndicator.getTabView(0).showNumber(6);
        alphaTabsIndicator.getTabView(1).showNumber(888);
        alphaTabsIndicator.getTabView(2).showNumber(88);
        alphaTabsIndicator.getTabView(3).showPoint();
        alphaTabsIndicator.setTabCurrenItem(0);
        alphaTabsIndicator.setOnTabChangedListner(new OnTabChangedListner() {
            @Override
            public void onTabSelected(int tabNum) {
                alphaTabsIndicator.setCurrenItem(tabNum);
            }
        });
//        LeakThread leakThread = new LeakThread();
//        leakThread.start();
    }
//    class LeakThread extends Thread {
//        @Override
//        public void run() {
//            try {
//                Thread.sleep(6 * 60 * 1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main_tab;
    }


    private class MainAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {

        private List<Fragment> fragments = new ArrayList<>();
        private String[] titles = {"首页", "附近", "看一看", "订单", "我的"};

        public MainAdapter(FragmentManager fm) {
            super(fm);
            fragments.add(HomeFragment.newInstance(titles[0], 0));
            fragments.add(NearFragment.newInstance(titles[1], 0));
//            fragments.add(FindFragmentTwo.newInstance(titles[2], 0));
            fragments.add(FindFragmentTwo.newInstance(titles[2], 0));
            //fragments.add(ActivityFragment.newInstance(titles[3], 0));
            fragments.add(OrderFragment.newInstance(titles[3], 0));
            fragments.add(MineFragment.newInstance(titles[4], 0));
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (0 == position) {
                alphaTabsIndicator.getTabView(0).showNumber(alphaTabsIndicator.getTabView(0).getBadgeNumber() - 1);
            } else if (2 == position) {
                alphaTabsIndicator.getCurrentItemView().removeShow();
            } else if (3 == position) {
                alphaTabsIndicator.getCurrentItemView().removeShow();
            } else if (4 == position) {
                alphaTabsIndicator.removeAllBadge();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!ishowed) {
            ishowed=true;
            startCount();
        }

    }

    private void startCount() {
        getWindow().getDecorView().postDelayed(runnable, 1000);
    }

    private PopWindow popWindow;
    private boolean ishowed = false;

    public void initPopWindow(View views) {
        popWindow = new PopWindow(context) {
            @Override
            public int getLayoutManu() {
                return R.layout.layout_pop_home_yuyue;
            }

            @Override
            public void getView(View view) {
                view.findViewById(R.id.pop_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        launch(ActivityDetailActivity.class);
                        popWindow.dismiss();
                    }
                });
                view.findViewById(R.id.img_dimiss).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWindow.dismiss();
                    }
                });
            }
        };
//        LogUtils.logi("----" + popWindow);
        popWindow.showAsDropDown(views);
    }

    private int totalCount = 0, time = 0;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time++;
            if (time > totalCount) {
                initPopWindow(pop);
            } else {
                startCount();
            }
        }
    };

    public static void setTab(int position) {
        alphaTabsIndicator.setTabCurrenItem(position);
    }
    private long mkeyTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mkeyTime) > 2000) {
                mkeyTime = System.currentTimeMillis();
                showToast("再按一次退出程序");
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
